Class Nur.MNISV3.Service.PatientInfo Extends %RegisteredObject [ Not ProcedureBlock ]
{

/// 就诊号
Parameter episodeID As STRING = "episodeID";

/// 病人姓名
Parameter name As STRING = "$p($g(^PAPER(personID,""""ALL"""")),""""^"""")";

/// 床位号
Parameter bedCode As STRING = "..GetBedCode(episodeID)";

/// 住院天数
Parameter inDays As STRING = "..PatInDays(episodeID)";

/// 费别
Parameter admReason As STRING = "..GetAdmReason(episodeID)";

/// 护理级别
Parameter careLevel As STRING = "..GetCareLevel(episodeID)";

/// 性别
Parameter sex As STRING = "$p($g(^CT(""""SEX"""",$p(personData,""""^"""",7))),""""^"""",2)";

/// 主管护士
Parameter mainNurse As STRING = "..GetMainNurse(episodeID)";

/// 医生
Parameter mainDoctor As STRING = "..GetMainDoctor(episodeID)";

/// 诊断
Parameter diagnosis As STRING = "..GetDiagnosis(episodeID)";

/// 入院
Parameter inHospDateTime As STRING = "..GetInHospDateTime(episodeID)";

/// 押金余额
Parameter balance As STRING = "..GetBalance(episodeID)";

/// 总费用
Parameter totalCost As STRING = "..GetTotalCost(episodeID)";

/// 医保号
Parameter insuranceCard As STRING = "..GetInsuranceCard(episodeID)";

/// 病案号
Parameter medicareNo As STRING = "..GetMedicareNo(episodeID)";

/// 身份证
Parameter personID As STRING = "..GetPersonID(episodeID)";

/// 科室
Parameter ctLocDesc As STRING = "..GetCtLocDesc(episodeID)";

/// 民族
Parameter nation As STRING = "..GetNation(episodeID)";

/// 住址
Parameter homeAddres As STRING = "..GetHomeAddres(episodeID)";

/// 联系人
Parameter patLinkman As STRING = "..GetPatLinkman(episodeID)";

/// 联系电话
Parameter telphone As STRING = "..GetTelphone(episodeID)";

/// 年龄
Parameter age As STRING = "..GetAge(episodeID)";

/// ID
Parameter regNo As STRING = "..GetRegNo(episodeID)";

/// 病区
Parameter wardDesc As STRING = "..GetWardDesc(episodeID)";

/// 病情
Parameter illState As STRING = "..GetIllState(episodeID)";

/// 手术
Parameter operation As STRING = "..GetOperation(episodeID)";

/// 入科
Parameter inDeptDateTime As STRING = "..GetInDeptDateTime(episodeID)";

/// 登记时间
Parameter regDateTime As STRING = "..GetRegDateTime(episodeID)";

Parameter motherTransLoc As STRING = "..MotherTransLoc(episodeID)";

/// 出院状态
Parameter dischargeStatus As STRING = "..GetCurrentDischargeStatus(episodeID)";

/// 房间
Parameter roomDesc As STRING = "..GetRoomDesc(episodeID)";

/// 术后天数
Parameter operLaterDays As STRING = "..GetOPDays(episodeID)";

/// 周天数
Parameter weekDays As STRING = "..GetWeekDays(episodeID)";

Parameter ifNewBaby As STRING = "..IfNewBaby(episodeID)";

Parameter orderID As STRING = "$o(^OEORD(0,""""Adm"""",episodeID,0))";

Parameter mainNurseID As STRING = "..GetMainNurseID(episodeID)";

Parameter mainDoctorID As STRING = "..GetMainDoctorID(episodeID)";

Parameter patientID As STRING = "patientID";

Parameter currLocID As STRING = "..GetCurrLocID(episodeID)";

Parameter currWardID As STRING = "$p($g(^PAADM(episodeID)),""""^"""",70)";

Parameter IPBookID As STRING = "..GetIPBookID(episodeID)";

Parameter IPAppBedID As STRING = "..GetIPAppBedID(episodeID)";

Parameter ifFirstToBed As STRING = "..IfFirstToBed(episodeID)";

Parameter careLevelColor As STRING = "..GetCareLevelColor(episodeID)";

/// Creator: 		pengjunfu
/// CreatDate: 		2017.1.4
/// Description: 	获取病人信息   
/// Input：			episodeID: 就诊号
/// Return：		{}
/// Other           w ##class(Nur.MNIS.Service.PatientInfo).GetPatient("330")
ClassMethod GetPatient(episodeID) As %ArrayOfDataTypes [ CodeMode = objectgenerator ]
{
#;	d %code.WriteLine(" s patient={}")
	d %code.WriteLine(" s patient=##class(%ArrayOfDataTypes).%New()")
	d %code.WriteLine(" s personID=$p($g(^PAADM(episodeID)),""^"",1)")
	d %code.WriteLine(" s patientID=personID")
	d %code.WriteLine(" s admData=$g(^PAADM(episodeID))")
	d %code.WriteLine(" q:(episodeID="""")!(admData="""") patient")
	d %code.WriteLine(" s personID=$p(admData,""^"",1)")
	d %code.WriteLine(" s personData=$g(^PAPER(personID,""ALL""))")
	d %code.WriteLine(" q:personData="""" patient")
	for i=1:1:%compiledclass.Parameters.Count()
	{

		s origin=%compiledclass.Parameters.GetAt(i).Origin
		continue:origin'=%compiledclass.Name
		s name=%compiledclass.Parameters.GetAt(i).Name
		s value=%compiledclass.Parameters.GetAt(i).Default
		s value="""s "_name_"="_value_""""
		d %code.WriteLine(" x "_value)
#;		d %code.WriteLine(" s patient."_name_"="_name)
		d %code.WriteLine(" d patient.SetAt("_name_","""_name_""")")
	}
	d %code.WriteLine(" q patient")
	q $$$OK
}

ClassMethod GetPatientForGetPatList(episodeID) As %ArrayOfDataTypes [ CodeMode = objectgenerator ]
{
#;	d %code.WriteLine(" s patient={}")
	d %code.WriteLine(" s patient=##class(%ArrayOfDataTypes).%New()")
	d %code.WriteLine(" s personID=$p($g(^PAADM(episodeID)),""^"",1)")
	d %code.WriteLine(" s patientID=personID")
	d %code.WriteLine(" s admData=$g(^PAADM(episodeID))")
	d %code.WriteLine(" q:(episodeID="""")!(admData="""") patient")
	d %code.WriteLine(" s personID=$p(admData,""^"",1)")
	d %code.WriteLine(" s personData=$g(^PAPER(personID,""ALL""))")
	d %code.WriteLine(" q:personData="""" patient")
	for i=1:1:%compiledclass.Parameters.Count()
	{

		s origin=%compiledclass.Parameters.GetAt(i).Origin
		continue:origin'=%compiledclass.Name
		s name=%compiledclass.Parameters.GetAt(i).Name
		continue:(("^"_$tr("ind,regNo,episodeId,bedCode,name,sex,inBedAll,manageInBed,todayOut,allOut,wait,careLevel,age,operation,illState,longOrd,tempOrd,newPatient,docDisch,arreag,gotAllergy,fever,criticalvalue,dangerous,epdFlag,epdNotReport,epdReport",",","^")_"^")'[("^"_name_"^"))
		s value=%compiledclass.Parameters.GetAt(i).Default
		s value="""s "_name_"="_value_""""
		d %code.WriteLine(" s pstart=+$zts*86400+$p($zts,"","",2)")
		d %code.WriteLine(" x "_value)
		d %code.WriteLine(" s pend=+$zts*86400+$p($zts,"","",2)")
		d %code.WriteLine(" d patient.SetAt("_name_","""_name_""")")
	}
	d %code.WriteLine(" q patient")
	q $$$OK
}

ClassMethod GetPatientWithoutIcon(episodeID) As %ArrayOfDataTypes [ CodeMode = objectgenerator ]
{
#;	d %code.WriteLine(" s patient={}")
	d %code.WriteLine(" s patient=##class(%ArrayOfDataTypes).%New()")
	d %code.WriteLine(" s personID=$p($g(^PAADM(episodeID)),""^"",1)")
	d %code.WriteLine(" s patientID=personID")
	d %code.WriteLine(" s admData=$g(^PAADM(episodeID))")
	d %code.WriteLine(" q:(episodeID="""")!(admData="""") patient")
	d %code.WriteLine(" s personID=$p(admData,""^"",1)")
	d %code.WriteLine(" s personData=$g(^PAPER(personID,""ALL""))")
	d %code.WriteLine(" q:personData="""" patient")
	for i=1:1:%compiledclass.Parameters.Count()
	{

		s origin=%compiledclass.Parameters.GetAt(i).Origin
		continue:origin'=%compiledclass.Name
		s name=%compiledclass.Parameters.GetAt(i).Name
		continue:(("^"_$tr("ind,regNo,episodeId,bedCode,name,sex,inBedAll,manageInBed,todayOut,allOut,wait,careLevel,age",",","^")_"^")'[("^"_name_"^"))
		s value=%compiledclass.Parameters.GetAt(i).Default
		s value="""s "_name_"="_value_""""
		d %code.WriteLine(" s pstart=+$zts*86400+$p($zts,"","",2)")
		d %code.WriteLine(" x "_value)
		d %code.WriteLine(" s pend=+$zts*86400+$p($zts,"","",2)")
		d %code.WriteLine(" d patient.SetAt("_name_","""_name_""")")
	}
	d %code.WriteLine(" q patient")
	q $$$OK
}

/// Creator: 		pengjunfu
/// CreatDate: 		2017.5.14
/// Description: 	获取getPatient返回的病人信息的每个key的name和desc
/// Input：			episodeID: 就诊号
/// Return：		{}
/// Other           w ##class(Nur.CommonInterface.Patient).getPatientKeys()
ClassMethod GetPatientKeys() As %ListOfDataTypes [ CodeMode = objectgenerator ]
{
#;	d %code.WriteLine(" s keys=[]")
	d %code.WriteLine(" s keys=##class(%ListOfDataTypes).%New()")
	for i=1:1:%compiledclass.Parameters.Count()
	{
		s origin=%compiledclass.Parameters.GetAt(i).Origin
		continue:origin'=%compiledclass.Name
		s description=%compiledclass.Parameters.GetAt(i).Description
		continue:description=""
		s name=%compiledclass.Parameters.GetAt(i).Name
		d %code.WriteLine(" s key=##class(%ArrayOfDataTypes).%New()")
#;		d %code.WriteLine(" s key.key="""_name_"""")	
#;		d %code.WriteLine(" s key.description="""_description_"""")
#;		d %code.WriteLine(" d keys.%Push(key)")
		d %code.WriteLine(" d key.SetAt("""_name_""",""key"")")
		d %code.WriteLine(" d key.SetAt("""_description_""",""description"")")
		d %code.WriteLine(" d keys.Insert(key)")
	}
	d %code.WriteLine(" q keys")
	q $$$OK
}

/// Creator: 		pengjunfu
/// CreatDate: 		2017.5.14
/// Description: 	获取病人费别
/// Input：			episodeID: 就诊号
/// Return：		
/// Other           w ##class(Nur.CommonInterface.Patient).getAdmReason("1")
ClassMethod GetAdmReason(episodeID)
{
	n (episodeID)
	s admReason=""
	s admReasonID=$P($g(^PAADM(episodeID,1)),"^",7)
	i admReasonID'="" s admReason=$P($g(^PAC("ADMREA",admReasonID)),"^",2)
	q admReason
}

/// Creator: 		pengjunfu
/// CreatDate: 		2017.5.14
/// Description: 	获取病人护理级别
/// Input：			episodeID: 就诊号
/// Return：		
/// Other           w ##class(Nur.CommonInterface.Patient).getCareLevel("1")
ClassMethod GetCareLevel(episodeID)
{
	n (episodeID)
	s tjhl=##class(web.DHCSETIMAGE).IfOrddailyExistByConfig(episodeID,"CRITICALCARE")
	q:tjhl=1 "特级护理"
	s yjhl=##class(web.DHCSETIMAGE).IfOrddailyExistByConfig(episodeID,"FIRSTCLSCARE")
	q:yjhl=1 "一级护理"
	s ejhl=##class(web.DHCSETIMAGE).IfOrddailyExistByConfig(episodeID,"SECONDCLSCARE")
	q:ejhl=1 "二级护理"
	s sjhl=##class(web.DHCSETIMAGE).IfOrddailyExistByConfig(episodeID,"THIRDCLSCARE")
	q:sjhl=1 "三级护理"
	q ""
}

/// Creator: 		pengjunfu
/// CreatDate: 		2017.5.12
/// Description: 	获取病人主管护士ID
/// Input：			episodeID: 就诊号
/// Return：		{name,episodeID,sex,bedCode}
/// Other           w ##class(Nur.CommonInterface.Patient).getMainNurseID("1")
ClassMethod GetMainNurseID(episodeID) As %ListOfDataTypes
{
	n (episodeID)
	s mainNurses=##class(%ListOfDataTypes).%New()
	s admExtID=""
	s admExtID=$O(^DHCPAAdm(0,"PAAdm",episodeID,admExtID),-1)
	i admExtID'="" d
	.s mainNurseID=$P($G(^DHCPAAdm(admExtID)),"^",2)
	.i +mainNurseID'=0 d
	..s mainNurseName=$P($G(^CTPCP(mainNurseID,1)),"^",2)
	..d mainNurses.Insert(+mainNurseID)
	.s nurseID=$P($G(^DHCPAAdm(admExtID)),"^",20)
	.i +nurseID'=0 d
	..s nurseName=$P($G(^CTPCP(nurseID,1)),"^",2)
	..d mainNurses.Insert(+nurseID)
	q mainNurses
}

/// Creator: 		pengjunfu
/// CreatDate: 		2017.5.12
/// Description: 	获取病人主管护士
/// Input：			episodeID: 就诊号
/// Return：	    主管护士姓名
/// Other           w ##class(Nur.CommonInterface.Patient).getMainNurse("1")
ClassMethod GetMainNurse(episodeID)
{
	n (episodeID)
	s mainNurses=""
	s admExtID=""
	s admExtID=$O(^DHCPAAdm(0,"PAAdm",episodeID,admExtID),-1)
	i admExtID'="" d
	.s mainNurseID=$P($G(^DHCPAAdm(admExtID)),"^",2)
	.i mainNurseID'="" d
	..s mainNurseName=$P($G(^CTPCP(mainNurseID,1)),"^",2)
	..s mainNurses=mainNurseName
	.s nurseID=$P($G(^DHCPAAdm(admExtID)),"^",20)
	.i nurseID'="" d
	..s nurseName=$P($G(^CTPCP(nurseID,1)),"^",2)
	..s mainNurses=mainNurses_" "_nurseName
	q mainNurses
}

/// Creator: 		pengjunfu
/// CreatDate: 		2017.5.12
/// Description: 	获取病人主管医生ID
/// Input：			episodeID: 就诊号
/// Return：		主管医生姓名
/// Other           w ##class(Nur.CommonInterface.Patient).getMainDoctorID("1")
ClassMethod GetMainDoctor(episodeID)
{
	n (episodeID)
	s mainDoctors=""
	s careProvID=$p(^PAADM(episodeID),"^",9)
	q:careProvID="" mainDoctors
	s docName=$p($g(^CTPCP(careProvID,1)),"^",2)
	s mainDoctors=docName
	q mainDoctors
}

/// Creator: 		pengjunfu
/// CreatDate: 		2017.5.12
/// Description: 	获取病人主管医生
/// Input：			episodeID: 就诊号
/// Return：		[id,id]
/// Other           w ##class(Nur.CommonInterface.Patient).getMainDoctor("1")
ClassMethod GetMainDoctorID(episodeID) As %ListOfDataTypes
{
	n (episodeID)
	s mainDoctors=##class(%ListOfDataTypes).%New()
	s careProvID=$p(^PAADM(episodeID),"^",9)
	q:careProvID="" mainDoctors
	s docName=$p($g(^CTPCP(careProvID,1)),"^",2)
	d mainDoctors.Insert(+careProvID)
	q mainDoctors
}

/// Creator: 		pengjunfu
/// CreatDate: 		2017.5.14
/// Description: 	获取当前床位号
/// Input：			episodeID: 就诊号
/// Return：		bedCode
/// Other           w ##class(Nur.CommonInterface.Patient).getBedCode("1")
ClassMethod GetBedCode(episodeID) As %String
{
	n (episodeID)
	s ^tmpWuJiang("episodeID")=episodeID
	s bedCode=""
	// --------------------------------wujiang 2020.4.18--------------------------
	// 查看病人就诊状态，如果是预约的，获取预约床号
	s visitStatus=$p(^PAADM(episodeID),"^",20)
	i visitStatus="P" {
		s bookID=$o(^DHCDocIPBK(0,"EpisodeIDTo",episodeID,""))
		q:bookID="" ""
		s IPAppID=$o(^DHCDocIPApp(0,"IPBookingID",bookID,""))
		q:IPAppID="" ""
		s bedID=$p($g(^DHCDocIPApp(IPAppID)),"^",9)
		q:bedID="" ""
		s bedCode=$p(^PAWARD(+bedID,"BED",$p(bedID,"||",2)),"^",1)
		q bedCode
	}
	// ---------------------------------------------------------------------------
	s bedID=$p(^PAADM(episodeID),"^",73)
	q:+bedID=0 ""
	s wardID=+bedID
	S bedSub=+$p(bedID,"||",2)
	s bedCode=$p($g(^PAWARD(wardID,"BED",bedSub)),"^",1)
	s bedCode=$replace(bedCode,"床","")
	q:bedCode'="" bedCode
	s sub="" f  s sub = $O(^PAADM(episodeID,"TRANS",sub),-1) q:(sub="")!($g(bedDr)'="")  d
	.s bedDr=$p(^PAADM(episodeID,"TRANS",sub),"^",8)
	.q:bedDr=""
	.s bedCode=$p(^PAADM(episodeID,"TRANS",sub),"^",33)
	.s bedCode=$replace(bedCode,"床","")
	q bedCode
}

/// Creator: 		pengjunfu
/// CreatDate: 		2017.1.5
/// Description: 	获取病人图标,改写epr.CTIconAssociation.GetImages
/// Input：			episodeID: 就诊号
/// Return：		[{title,iconSrc,linkUrl,category,sequence}]
/// Other           w ##class(Nur.CommonInterface.Patient).getBedImages("50092186")
ClassMethod GetBedImages(episodeID, profileID = 1)
{
	n (episodeID, profileID,%session,%request)
	s images=##class(%ListOfDataTypes).%New()
	s profileObj=##Class(epr.CTIconProfile).%OpenId(profileID)
	s EpisodeID=episodeID
	s PatientID=$p($g(^PAADM(episodeID)),"^",1)	
	s mradm=$P($g(^PAADM(episodeID)),"^",61)
	s forceRefresh="true"
	if $IsObject(profileObj){
		
		s top=40,left=40,width=640,height=480
		s count=profileObj.Items.Count()
		for i=1:1:count {
			s itemObj=profileObj.Items.GetAt(i)
			s iconObj=itemObj.IconDR
			if $IsObject(iconObj) {
				s img=0
				s title=""
				continue:iconObj.CondExpr=""
				
				d ##class(websys.Conversions).expressionExecute(iconObj.CondExpr)
	
				s code=iconObj.Code
				continue:code="ifadmit" //过滤基本信息
				continue:img'=1 
				continue:iconObj.Icon=""
				
				s image=##class(%ArrayOfDataTypes).%New()
				If title="" s title=iconObj.Description
				// s iconHTML="<IMG align='top' SRC='../images/"_iconObj.Icon_"' title='"_title_"' border=0></A>"

				
				s linkUrl=itemObj.LinkUrl
				s linkComponent=itemObj.LinkComponent
				s linkExpression=##class(websys.Conversions).expressionEvaluate(itemObj.LinkExpression)
				s linkChartBookDR=itemObj.LinkChartBookDR
				s sequence=itemObj.Sequence
				
				if linkUrl'="" {
					s linkUrl=linkUrl_"?"
					i linkComponent'="" s linkUrl=linkUrl_"WEBSYS.TCOMPONENT="_linkComponent
					s linkUrl=linkUrl_"&FromIconProfile="_profileID_"&EpisodeID="_episodeID_linkExpression   //_"&CONTEXT="_$g(%session.Data("CONTEXT"))
					
				}
				
#;				s image.category =iconObj.CondDescription //描述当类别用
#;				s image.title=title
#;				s image.originTitle=iconObj.Description
#;				s image.iconSrc=iconObj.Icon
#;				s image.linkUrl=linkUrl
#;				s image.sequence=sequence
				;b
				d image.SetAt(iconObj.CondDescription,"category")
				d image.SetAt(title,"title")
				d image.SetAt(iconObj.Description,"originTitle")
				d image.SetAt(iconObj.Icon,"iconSrc")
				d image.SetAt(linkUrl,"linkUrl")
				d image.SetAt(sequence,"sequence")
				d image.SetAt("true","visible")
				i iconObj.Description["级护理" d image.SetAt("false","visible")
				s location=""
				i (iconObj.CondExpr["),top=") d
				.s location="top="_$p(iconObj.CondExpr,"),top=",2)
				i (iconObj.CondExpr["),left=") d
				.s location="left="_$p(iconObj.CondExpr,"),left=",2)
				i (iconObj.CondExpr["),width=") d
				.s location="width="_$p(iconObj.CondExpr,"),width=",2)
				i (iconObj.CondExpr["),height=") d
				.s location="height="_$p(iconObj.CondExpr,"),height=",2)				
				d image.SetAt("false","hisui")
				i location["hisui=""true""" d
				.d image.SetAt("true","hisui")
				.s location=$replace(location,",hisui=""true""","")
				.s location=$replace(location,"hisui=""true""","")
				d image.SetAt(location,"location")				
				if sequence="" {
					s iconArr(i)=image
				}else{
					s iconArrFirst(sequence)=image
				}
			}
		}
		
		//插入组别信息, 方便前端一起处理  --pengjunfu 
		s bedGroupImageInfo=..GetBedGroupImageInfo(episodeID)
		//i bedGroupImageInfo.%Size()'=0 d
		.//d images.%Push(bedGroupImageInfo)
		i bedGroupImageInfo.Count()'=0 d
		.d bedGroupImageInfo.SetAt("true","visible")
		.d images.Insert(bedGroupImageInfo)
		
		s seq=$o(iconArrFirst(""))
		while (seq'=""){
			//d images.%Push(iconArrFirst(seq))
			d images.Insert(iconArrFirst(seq))
			s seq=$o(iconArrFirst(seq))
			q:seq=""
		}
		s seq=$o(iconArr(""))
		while (seq'=""){
			//d images.%Push(iconArr(seq))
			d images.Insert(iconArr(seq))
			s seq=$o(iconArr(seq))
			q:seq=""
		}
	}
	q images
}

/// Creator: 		pengjunfu
/// CreatDate: 		2017.11.18
/// Description: 	批量获取病人图标,改写epr.CTIconAssociation.GetImages
/// Input：			episodeIDString: 就诊号串
/// Return：		[[image,image,...],...]
/// Other           w ##class(Nur.CommonInterface.Patient).getChunkImages("1")
ClassMethod GetChunkImages(episodeIDString)
{
	n (episodeIDString,%session,%request)
	s result=##class(%ListOfDataTypes).%New()
	s count=$l(episodeIDString,"^")
	f i=1:1:count 
	{
		s episodeID=$p(episodeIDString,"^",i)
		s images=..GetBedImages(episodeID)
		d result.Insert(images)
	}
	q ##class(Nur.JSON).EncodeByWrite(result)
}

/// Creator:      pengjunfu
/// CreateDate:   2017.5.31
/// Description:  获取专业组分类的image信息
/// Input:        episodeID:就诊ID
/// Return:       
/// Other:        w ##class(Nur.CommonInterface.Patient).getBedGroupImageInfo("48")
ClassMethod GetBedGroupImageInfo(episodeID)
{
	n (episodeID)
	//s image={}
	s image=##class(%ArrayOfDataTypes).%New()
	s bedID=$p(^PAADM(episodeID),"^",73)
	s bedGroupCode="",bedGroupDesc=""
	s bedGroupBedDR=""
	s:bedID'="" bedGroupBedDR=$o(^DHCWardProGroupBed(0,"PacBedDr",bedID,0))
	i bedGroupBedDR'="" d
	.s bedGroupDR=+^DHCWardProGroupBed(bedGroupBedDR)
	.s bedGroupData=$g(^DHCWardProGroup(bedGroupDR))
	.q:bedGroupData=""
	.s bedGroupDesc=$p(bedGroupData,"^",2)
	.s bedGroupCode=$p(bedGroupData,"^",1)
	.s active=$p(bedGroupData,"^",3)
	.q:active'="Y"
	.;i bedGroupCode'["组" s bedGroupCode=bedGroupCode_"组"
	.//s image.category ="组别"
	.//s image.title=bedGroupCode
    .//s image.originTitle=bedGroupCode
	.//s image.iconSrc=""
	.//s image.linkUrl=""
	.//s image.sequence=0
	.d image.SetAt("组别","category")
	.d image.SetAt(bedGroupDesc,"title")
    .d image.SetAt(bedGroupDesc,"originTitle") 
	.d image.SetAt("","iconSrc")
	.d image.SetAt("","linkUrl")
	.d image.SetAt(0,"sequence")
	q image
}

/// Creator:      SongChao
/// CreateDate:   2017.5.18
/// Description:  获取病人诊断信息
/// Input:        episodeID:就诊ID,typ:诊断类型
/// Return:       病人诊断,多个诊断用";"分割
/// Other:        w ##class(Nur.CommonInterface.Patient).getDiagnosis("48")
ClassMethod GetDiagnosis(episodeID)
{
	n (episodeID)
	s diagnosis=""
    q:episodeID="" diagnosis
    s mrAdmID=$P(^PAADM(episodeID),"^",61)
    q:mrAdmID="" diagnosis
    s diagnosis =##class(web.DHCDocInPatientList).GetMRAdmDiagnosis(mrAdmID)
    /*f mrID=1:1:$g(^MR(episodeID,"DIA",0)) d
    .s icdr=$p($g(^MR(episodeID,"DIA",mrID)),"^",1)
    .i icdr'="" d
  	..s desc=$P($g(^MRC("ID",icdr)),"^",2)
    ..i diagnosis="" s diagnosis=desc
    ..e  s diagnosis=diagnosis_";"_desc*/
    s diagnosis=$zcvt(diagnosis,"O","JS")
    q diagnosis
}

/// Creator:      SongChao
/// CreateDate:   2017.5.18
/// Description:  获取病人入院时间
/// Input:        episodeID:就诊号
/// Return:       病人入院时间
/// Other:        w ##class(Nur.CommonInterface.Patient).getInHospDateTime("48")
ClassMethod GetInHospDateTime(episodeID)
{
	n (episodeID)
	s inHospDateTime=""
	q:episodeID="" inHospDateTime
	s admType=$p($g(^PAADM(episodeID)),"^",2)
	s inHospDate=$p($g(^PAADM(episodeID,"DHC")),"^",31)
	s inHospTime=$p($g(^PAADM(episodeID,"DHC")),"^",32)
	i inHospDate'="" s inHospDate=##class(websys.Conversions).DateLogicalToHtml(inHospDate) ;$ZD(inHospDate,3) ;
	i inHospTime'="" s inHospTime=$ZT(inHospTime)
	s inHospDateTime=$g(inHospDate)_" "_inHospTime
	q inHospDateTime
}

/// Creator:      SongChao
/// CreateDate:   2017.5.18
/// Description:  获取病人押金余额
/// Input:        episodeID:就诊号
/// Return:
/// Other:        w ##class(Nur.CommonInterface.Patient).getBalance("48")
ClassMethod GetBalance(episodeID)
{
	n (episodeID)
	s balance=""
	q:episodeID="" balance
	s FeeInfo=##Class(web.DHCBillInterface).IGetDepAndBalnace(episodeID)
	s total=+$p(FeeInfo,"^",3)
    s depos=+$p(FeeInfo,"^",1)
    s balance=+$p(FeeInfo,"^",2) ;(+$G(depos))-(+$G(total))
    s balance=$fn(balance,"",2)
    //医生站接口
    s balance=##class(web.DHCDocOrderCommon).GetCurrentDeposit($g(episodeID))
 	q balance
}

/// Creator:      SongChao
/// CreateDate:   2017.5.18
/// Description:  获取总费用
/// Input:        episodeID:就诊号
/// Return:       总花费
/// Other:        w ##class(Nur.CommonInterface.Patient).getTotalCost("172")
ClassMethod GetTotalCost(episodeID)
{
	n (episodeID)
	s totalCost=""
	q:episodeID="" totalCost
	s FeeInfo=##Class(web.DHCBillInterface).IGetDepAndBalnace(episodeID)
	s totalCost=+$p($g(FeeInfo),"^",3)
 	q totalCost
}

/// Creator:      SongChao
/// CreateDate:   2017.5.24
/// Description:  获取医保号
/// Input:
/// Return:
/// Other:
ClassMethod GetInsuranceCard(episodeID)
{
	n (episodeID)
	s insuranceCard=""
	s PAPMIID=+$g(^PAADM(episodeID))
	s insuranceCard= $p($g(^PAPER(PAPMIID,"PAT",3)),"^",12)   //modefy by zhangdongliang at 2017-08-07 需求序号: 421565 医保手册号取PAPMI_HealthFundNo字段
	q insuranceCard
}

/// Creator:      SongChao
/// CreateDate:   2017.5.24
/// Description:  获取病案号
/// Input:
/// Return:
/// Other: ##class(Nur.CommonInterface.Patient).getMedicareNo(1056)
ClassMethod GetMedicareNo(episodeID)
{
	n (episodeID)
	s medicareNo=""
	q:episodeID="" medicareNo
	s papmiID=+$g(^PAADM(+episodeID))
    q:papmiID=0 ""
	s admType=$p(^PAADM(episodeID),"^",2)
	s ctlocID=$p(^PAADM(episodeID),"^",4)
    s hospital=$p($g(^CTLOC(+ctlocID)),"^",22)
    s errmsg=""
	s medicareNo=##class(DHCWMR.IO.OutService).IGetMrNoByPatientID(papmiID,admType,hospital,.errmsg) //调用病案接口取病案号
	;安贞医院急诊留观,病案不应该产生病案号,应医院要求特殊处理
	;s CtlocDr=$p($g(^PAADM(+episodeID)),"^",4)
	;i CtlocDr'="" s CtlocDesc=$p($g(^CTLOC(CtlocDr)),"^",2)
	;e  s CtlocDesc=""
	;i ($g(CtlocDr)=2)&&($g(CtlocDesc)="急诊科") s medicareNo=""
#;	i medicareNo="" s medicareNo=..GetRegNo(episodeID)
#;	;安贞婴儿病案号同母亲
#;	s motherAdm=$p($g(^PAADM(episodeID)),"^",75)
#;	i $g(motherAdm)'="" d
#;	.s papmiID=+$g(^PAADM(+motherAdm))
#;    .q:papmiID=0
#;	.s admType=$p(^PAADM(motherAdm),"^",2)
#;	.s ctlocID=$p(^PAADM(motherAdm),"^",4)
#;    .s hospital=$p($g(^CTLOC(+ctlocID)),"^",22)
#;	.s medicareNo=##class(DHCWMR.IO.OutService).IGetMrNoByPatientID(papmiID,admType,hospital,.errmsg) //调用病案接口取病案号
	q medicareNo
}

/// Creator:      SongChao
/// CreateDate:   2017.5.24
/// Description:  获取病人身份证号
/// Input:
/// Return:
/// Other:
ClassMethod GetPersonID(episodeID)
{
	n (episodeID)
	s personID=""
	q:episodeID="" personID
	s papmiID=+$g(^PAADM(+episodeID))
    q:papmiID=0 ""
    s personID=$p($G(^PAPER(papmiID,"ALL")),"^",9)  //身份证
    s:personID'="" personID=personID_" "
    q personID
}

/// Creator:      SongChao
/// CreateDate:   2017.5.24
/// Description:  获取病人科室
/// Input:
/// Return:
/// Other:
ClassMethod GetCtLocDesc(episodeID)
{
	n (episodeID)
	s ctLocDesc=""
	q:episodeID="" ctlocDesc
	s ctLocID=$p(^PAADM(episodeID),"^",4)
	s ctLocDesc=$p(^CTLOC(+ctLocID),"^",2)	
    i (ctLocDesc["-")&&($p(ctLocDesc,"-",2)'="") s ctLocDesc=$p(ctLocDesc,"-",2)
    q ctLocDesc
}

/// Creator:      SongChao
/// CreateDate:   2017.5.24
/// Description:  获取病人民族
/// Input:        
/// Return:
/// Other:
ClassMethod GetNation(episodeID)
{
	n (episodeID)
	q:episodeID="" ""
	s nation=""
	s papmiID=+$g(^PAADM(+episodeID))
	s nationDr=$p($g(^PAPER(papmiID,"PER",2)),"^",1)
    i nationDr'="" s nation=$P(^CT("NAT",nationDr),"^",2)
    q nation
}

/// Creator:      SongChao
/// CreateDate:   2017.5.24
/// Description:  获取病人住址
/// Input:
/// Return:
/// Other:
ClassMethod GetHomeAddres(episodeID)
{
	n (episodeID)
	q:episodeID="" ""
	s homeAddres=""
	s papmiID=+$g(^PAADM(+episodeID))
	s homeAddres=$g(^PAPER(papmiID,"PER","ADD",1))     // 住址
	q homeAddres
}

/// Creator:      SongChao
/// CreateDate:   2017.5.24
/// Description:  获取联系人
/// Input:
/// Return:
/// Other:
ClassMethod GetPatLinkman(episodeID)
{
	n (episodeID)
	q:episodeID="" ""
	s linkman=""
	s papmiID=+$g(^PAADM(+episodeID))
	s linkman=$p($g(^PAPER(papmiID,"PER",2)),"^",13) //联系人
	q linkman
}

/// Creator:      SongChao
/// CreateDate:   2017.5.24
/// Description:  获取联系电话
/// Input:
/// Return:
/// Other:    ##class(Nur.CommonInterface.Patient).getTelphone(16)
ClassMethod GetTelphone(episodeID)
{
	n (episodeID)
	q:episodeID=""
	s telphone=""
	s papmiID=+$g(^PAADM(+episodeID))
	s handtel=$p($g(^PAPER(papmiID,"PER",4)),"^",21)   //手机
	s telphone=handtel
	q:handtel'="" telphone
	s homeTel=$p($g(^PAPER(papmiID,"PER",1)),"^",11)   //家庭电话
	s telphone=homeTel
	q:homeTel'="" telphone
    s workTel=$p($g(^PAPER(papmiID,"PER",1)),"^",9)    //工作电话
    s telphone=workTel
    q:workTel'="" telphone
    q telphone
}

/// Creator:      SongChao
/// CreateDate:   2017.6.2
/// Description:  获取病人年龄
/// Input:        episodeID:就诊号
/// Return:
/// Other:        ##class(Nur.CommonInterface.Patient).getAge(16)
ClassMethod GetAge(episodeID)
{
	n (episodeID)
	q:episodeID="" ""
	s age=""
	s papmiID=+$g(^PAADM(+episodeID))
	s age = ##class(web.DHCBillInterface).GetPapmiAge(papmiID,episodeID)
	q age
}

/// Creator:      SongChao
/// CreateDate:   2017.6.2
/// Description:  获取病人登记号
/// Input:        episodeID:就诊号
/// Return:
/// Other:        ##class(Nur.MNIS.Service.PatientInfo).GetRegNo(15)
ClassMethod GetRegNo(EpisodeID)
{
	q ##class(web.DHCCLCom).GetRegNobyEpisodeID(EpisodeID)
}

/// Creator:      SongChao
/// CreateDate:   2017.6.20
/// Description:  获得病人当前所在病区
/// Input:        episodeID
/// Return:       
/// Other:
ClassMethod GetWardDesc(episodeID)
{
	q:$g(episodeID)="" ""
 	q:'$d(^PAADM(episodeID)) ""
 	s ward=""
 	s wardID = $p(^PAADM(episodeID),"^",70)
 	s:wardID'="" ward = $p($g(^PAWARD(wardID)),"^",2)
	i ward["-" s ward=$p(ward,"-",2)
 	q ward
}

/// Creator:      SongChao
/// CreateDate:   2017.11.13
/// Description:  获得病人病情
/// Input:        episodeID
/// Return:       
/// Other:  ##class(Nur.CommonInterface.Patient).getIllState(322)
ClassMethod GetIllState(episodeID)
{
	n (episodeID)
	s seriously=##class(web.DHCSETIMAGE).IfOrddailyExistByConfig(episodeID,"SERIOUSLY")
	q:seriously=1 "病重"
	s critically=##class(web.DHCSETIMAGE).IfOrddailyExistByConfig(episodeID,"CRITICALLY")
	q:critically=1 "病危"
	q "普通"
}

/// Creator:      SongChao
/// CreateDate:   2017.11.13
/// Description:  获得病人手术
/// Input:        episodeID
/// Return:       
/// Other:  ##class(Nur.CommonInterface.Patient).getOperation(322)
ClassMethod GetOperation(episodeID)
{
	n (episodeID)
	s ret=""
	Set $ZTrap = "ErrorHandle"
	s ret=##class(web.DHCANAdaptor).GetAdmOperInfoForEMR(episodeID)
	s len=$l(ret,"^")
	i len>0 s ret=$p($p($p(ret,"^",len),"#",9),"/")
	q ret
ErrorHandle
	q ret
}

/// Creator：   SongChao
/// CreatDate： 2017.11.17
/// Description： 
/// Table： 
/// Input：
/// Output：
/// Others：  ##class(Nur.CommonInterface.Patient).getInDeptDateTime(2193992)
ClassMethod GetInDeptDateTime(episodeID)
{
	n (episodeID)
	s inDeptDateTime=""
	s currentWardID = $P(^PAADM(episodeID),"^",70)
	q:currentWardID="" inDeptDateTime
	s firstBedFlag=0
	s transID = ""
	f  s transID=$o(^PAADM(episodeID,"TRANS",transID)) q:((transID="")!(firstBedFlag=1))  d
	.s transGlobal=^PAADM(episodeID,"TRANS",transID)
	.s transBedDr = $p(transGlobal,"^",8)
	.q:$g(transBedDr)=""
	.q:(+transBedDr'=currentWardID)
	.s date = $p(transGlobal,"^",1)
	.s time = $p(transGlobal,"^",2)
	.s inDeptDateTime = ##class(websys.Conversions).DateLogicalToHtml(date)_" "_$zt(time,2)
	.s firstBedFlag=1
	q inDeptDateTime
}

/// Creator:      SongChao
/// CreateDate:   2017.8.8
/// Description:  病人住院天数
/// Input:        episodeID:就诊号
/// Return:       ret:天数
/// Other:  ##Class(Nur.CommonInterface.Patient).PatInDays(318)
ClassMethod PatInDays(episodeID)
{
	n (episodeID)
	q:episodeID="" "就诊号为空"
	s ret=0
	s AdmInOutDate=##class(web.UDHCJFBaseCommon).GetAdmInOutDatebyEpisodeID(episodeID)
	s ret=$P(AdmInOutDate,"^",3)	
	i ret=0 s ret="0 "
	q ret
}

/// Creator:      SongChao
/// CreateDate:   2018.1.6
/// Description:  病人入院登记时间
/// Input:		  episodeID:就诊号
/// Return:
/// Other:        w ##class(Nur.CommonInterface.Patient).getRegDateTime(16).%ToJSON()
ClassMethod GetRegDateTime(episodeID)
{
	n (episodeID)
	s regDateTime=""
	s regDateH = $p($g(^PAADM(episodeID)),"^",6)
	s regDate = ##class(websys.Conversions).DateLogicalToHtml(regDateH) ;$zd(regDateH,3)
	s regTimeH = $p($g(^PAADM(episodeID)),"^",7)
	s regTime = $zt(regTimeH)
	s regDateTime = regDate_" "_regTime
	q regDateTime
}

/// Creator：   SongChao
/// CreatDate： 2018.01.10
/// Description： 母亲是否转到其他科室
/// Table： 
/// Input：
/// Output：
/// Others：  ##class(Nur.MNIS.Service.PatientInfo).MotherTransLoc(50059285)
ClassMethod MotherTransLoc(episodeID)
{
	n (episodeID)
	s ret=""
	q ret
	q:episodeID="" ret
	s babyWard=$p(^PAADM(episodeID),"^",70)	
	s babyBed=$p(^PAADM(episodeID),"^",73)	
	s motherAdm= $p(^PAADM(episodeID),"^",75)
	q:motherAdm="" ret
	s motherCurrWard = $p(^PAADM(motherAdm),"^",70)	
	s motherBed=$p(^PAADM(motherAdm),"^",73)
	i ($g(motherBed)'=""&&(motherCurrWard=babyWard)) s ret="母婴同室"	
	i (motherCurrWard'=babyWard) s ret="母亲转科"
	i (($g(motherCurrWard)=babyWard)&&($g(motherBed)="")&&($g(babyBed)'="")) s ret="母亲转科"
	q ret
}

/// Creator:      SongChao
/// CreateDate:   2018.6.1
/// Description:  获得病人的转科记录
/// Table:        Nur_Data.TransRecord
/// Input:        episodeID
/// Return:       [{transID,transDate,transTime,transType,transUser,transFrom,transTo,transState}]
/// Other:        w ##class(Nur.CommonInterface.Patient).getTransRecord(50125913)
ClassMethod GetTransRecord(episodeID)
{
	n (episodeID)
	s transRecords=##class(%ListOfDataTypes).%New()	
	s reverseTransRecords=##class(%ListOfDataTypes).%New()	
	q:episodeID="" reverseTransRecords
	s admRecord=##class(%ArrayOfDataTypes).%New()
	s admRecordTransType="入院登记"	
	s ifNewBaby=..IfNewBaby(episodeID)
	i ifNewBaby="Y" s admRecordTransType="新生儿分娩"
	d admRecord.SetAt(admRecordTransType,"transType")
	s transGlobal=^PAADM(episodeID,"TRANS",1)
	s amdLocDR=$p(transGlobal,"^",6)  ;$p(^PAADM(episodeID),"^",4)
	d admRecord.SetAt($p($g(^SSU("SSUSR",$p(transGlobal,"^",16))),"^",2),"transUser")  ;$p(^PAADM(episodeID),"^",43))
	d admRecord.SetAt(##class(websys.Conversions).DateLogicalToHtml($p(transGlobal,"^",1)),"transDate")  ;$p(^PAADM(episodeID),"^",6);d transRecord.SetAt($zd($p(transGlobal,"^",1),3),"transDate") ;
    d admRecord.SetAt(##class(websys.Conversions).TimeLogicalToHtml($p(transGlobal,"^",2)),"transTime")  ;$p(^PAADM(episodeID),"^",7)
    d admRecord.SetAt($p($g(^CTLOC(amdLocDR)),"^",2),"transTo")
    d admRecord.SetAt("","transFrom")
    d transRecords.Insert(admRecord)
    s firstToBed=0
	s sub="" 
	f  {
		s sub=$O(^Nur.Data.TransRecordI("TransDataState"," "_episodeID," N",sub)) 
		q:sub=""
		s transRecord=##class(%ArrayOfDataTypes).%New()
		d transRecord.SetAt(sub,"transID")
		d transRecord.SetAt($p($g(^SSU("SSUSR",$LG(^Nur.Data.TransRecordD(sub),14))),"^",2),"transUser")
		d transRecord.SetAt(##class(websys.Conversions).DateLogicalToHtml($LG(^Nur.Data.TransRecordD(sub),12)),"transDate")  ;d transRecord.SetAt($zd($p(transGlobal,"^",1),3),"transDate") ;
    	d transRecord.SetAt(##class(websys.Conversions).TimeLogicalToHtml($LG(^Nur.Data.TransRecordD(sub),13)),"transTime")
		s transType=$LG(^Nur.Data.TransRecordD(sub),3)
		i (transType="B")&&(firstToBed=0){
			d transRecord.SetAt("首次分床","transType")
			d transRecord.SetAt($LG(^Nur.Data.TransRecordD(sub),11),"transTo")
    		d transRecord.SetAt("","transFrom")
    		s firstToBed=1
		}
		elseif((transType="B")&&(firstToBed=1)) {
			d transRecord.SetAt("分床","transType")
			d transRecord.SetAt($LG(^Nur.Data.TransRecordD(sub),11),"transTo")
			s bedDesc=$LG(^Nur.Data.TransRecordD(sub),9)
			i bedDesc="" s bedDesc="等候区"
    		d transRecord.SetAt(bedDesc,"transFrom")
		}
		elseif(transType="L"){
			d transRecord.SetAt("转科","transType")
			d transRecord.SetAt($p($g(^CTLOC($LG(^Nur.Data.TransRecordD(sub),5))),"^",2),"transTo")
    		d transRecord.SetAt($p($g(^CTLOC($LG(^Nur.Data.TransRecordD(sub),4))),"^",2),"transFrom")
	   	}
	   	elseif(transType="W"){
			d transRecord.SetAt("转病区","transType")
			d transRecord.SetAt($p($g(^PAWARD($LG(^Nur.Data.TransRecordD(sub),7))),"^",2),"transTo")
    		d transRecord.SetAt($p($g(^PAWARD($LG(^Nur.Data.TransRecordD(sub),6))),"^",2),"transFrom")
	   	}
	   	d transRecords.Insert(transRecord)
	}
	f tri=(transRecords.Count()):-1:1 d
	.d reverseTransRecords.Insert(transRecords.GetAt(tri))
    q ##class(Nur.JSON).EncodeByWrite(reverseTransRecords)
}

/// Creator:      SongChao
/// CreateDate:   2017.6.9
/// Description:  获得病人的转科记录()
/// Table:        PA_AdmTransaction
/// Input:        episodeID
/// Return:       [{transID,transDate,transTime,transType,transUser,transFrom,transTo,transState}]
/// Other:        w ##class(Nur.CommonInterface.Patient).getTransRecord(50125913)
ClassMethod GetTransRecordBAK(episodeID)
{
	n (episodeID)
	s transRecords=##class(%ListOfDataTypes).%New()
	s reverseTransRecords=##class(%ListOfDataTypes).%New()
	i episodeID="" q transRecords
    s (preWardDR,preLoc,preBed)=""
    s alreadyToBed="false"
    s sub=0 f  s sub = $O(^PAADM(episodeID,"TRANS",sub)) q:sub=""  d
    .s transRecord=##class(%ArrayOfDataTypes).%New()
    .d transRecord.SetAt(sub,"transID")
    .s transGlobal=^PAADM(episodeID,"TRANS",sub)
    .s transDR= $p(transGlobal,"^",21)
    .s transTypeCode=""
    .s:transDR'="" transTypeCode=$p(^PAC("TRANSTYP",transDR),"^",1)
	.s ssuserDR = $p(transGlobal,"^",16)
	.d transRecord.SetAt($s(ssuserDR'="":$p($g(^SSU("SSUSR",ssuserDR)),"^",2),1:""),"transUser")
    .d transRecord.SetAt(##class(websys.Conversions).DateLogicalToHtml($p(transGlobal,"^",1)),"transDate")  ;d transRecord.SetAt($zd($p(transGlobal,"^",1),3),"transDate") ;
    .d transRecord.SetAt(##class(websys.Conversions).TimeLogicalToHtml($p(transGlobal,"^",2)),"transTime")
    .s statusDR=$p(transGlobal,"^",12) ;TRANS_Status_DRPACTransfRequestStatus
    .i statusDR'="" d transRecord.SetAt($p(^PAC("REQST",statusDR),"^",2),"transState")
	.d transRecord.SetAt("","transType")
	.d transRecord.SetAt("","transTo")
	.d transRecord.SetAt("","transFrom")
    .s ctlocDR=$p(transGlobal,"^",6)    
    .s wardDR = $p(transGlobal,"^",9)
    .i (transTypeCode="")&&(ctlocDR'="") d
    ..d transRecord.SetAt("入院登记","transType")
    ..d transRecord.SetAt($p($g(^CTLOC(ctlocDR)),"^",2),"transTo")
    ..s preLoc=ctlocDR
    .s bedDR=$p(transGlobal,"^",8)
    .i (transTypeCode="M") d    
    ..s:ctlocDR'="" preLoc=ctlocDR
   	..i (wardDR'="")&&(preWardDR="")&&(alreadyToBed="false")&&(bedDR'="") d
   	...d transRecord.SetAt("首次分床","transType")
   	...s bedDesc=##class(Nur.CommonInterface.Bed).getBedCode(episodeID,bedDR)
    ...d transRecord.SetAt(bedDesc,"transTo")
    ...s preBed=bedDR    
    ...s alreadyToBed="true"
    ...s preWardDR=wardDR
    ..i (preWardDR'="")&&(preWardDR=wardDR) d
    ...q:(bedDR'["||")||((preBed'["||"))||(preBed=bedDR)    
    ...d transRecord.SetAt("分床","transType")
    ...d transRecord.SetAt(##class(Nur.CommonInterface.Bed).getBedCode(episodeID,bedDR),"transTo")
    ...d transRecord.SetAt(##class(Nur.CommonInterface.Bed).getBedCode(episodeID,preBed),"transFrom")
    .s:bedDR'="" preBed=bedDR
    .i (transTypeCode="M")&&(wardDR'="")&&(preWardDR'="")&&(preWardDR'=wardDR) d
    ..d transRecord.SetAt("转病区","transType")
    ..d transRecord.SetAt($p($g(^PAWARD(wardDR)),"^",2),"transTo")
    ..d transRecord.SetAt($p($g(^PAWARD(preWardDR)),"^",2),"transFrom")
    ..s preWardDR=wardDR
    .i (transTypeCode="T") d
    ..i (preLoc'="")&&(ctlocDR'="")&&(preLoc'=ctlocDR) d
    ...d transRecord.SetAt("转科","transType")
    ...d transRecord.SetAt($p($g(^CTLOC(ctlocDR)),"^",2),"transTo")
    ...d transRecord.SetAt($p($g(^CTLOC(preLoc)),"^",2),"transFrom")
    ..s preLoc=ctlocDR
    ..s:wardDR'="" preWardDR=wardDR
    .q:(transRecord.GetAt("transTo")="")&&(transRecord.GetAt("transFrom")="")
 	.d transRecords.Insert(transRecord)
 	f tri=(transRecords.Count()):-1:1 d
	.d reverseTransRecords.Insert(transRecords.GetAt(tri))
    q ##class(Nur.JSON).EncodeByWrite(reverseTransRecords)
}

/// Creator:      SongChao
/// CreateDate:   2017.6.21
/// Description:  修改病人主管医生和护士
/// Input:		  episodeID:就诊号,mianDocID:主管医生,mainNurIDArray:主管护士
/// Return:
/// Other:        w ##class(Nur.CommonInterface.Patient).updateMainDocNur(16).%ToJSON()
ClassMethod UpdateMainDocNur(episodeID, mainDocID, mainNurIDArray, userID = "")
{
	n (episodeID, mainDocID, mainNurIDArray,userID)
	s ^TMPSC("updateMainDocNur")=$lb(episodeID, mainDocID, mainNurIDArray, userID)
	s ret=##class(%ArrayOfDataTypes).%New()
	d ret.SetAt(0,"status")
	q:episodeID="" ret
	s ctcpID=$P($g(^SSU("SSUSR",userID)),"^",14)
    s ctcptID=$P($G(^CTPCP(ctcpID,1)),"^",4) 
    s ctcptType=$P($g(^CT("CPT",ctcptID)),"^",4)
    i ctcptType'="NURSE" d ret.SetAt("医护人员类型不是NURSE不能更新主管医生护士!","status")
	q:ctcptType'="NURSE" ret
	ts
	s currMainDoc=$p($g(^PAADM(episodeID)),"^",9)
	i ((mainDocID'="")&&(currMainDoc'=$g(mainDocID))) {
		s retCode=##class(Nur.CommonInterface.Bed).UpdateMainDoc(episodeID, $g(mainDocID),userID,.errmsg)
		;s retCode=##class(web.DHCMainNurse).UpdateDoc(episodeID, mainDocID , "", userID)
		i +retCode'=0 d ret.SetAt("修改病人主管医生出错！","status") q ret
		s curAdmLocID = $p($g(^PAADM(episodeID)),"^",4)
		s userID=$o(^SSU("SSUSR",0,"CTPCP",$g(mainDocID),""))
		d ##class(web.DHCDocThreeCheckListAdm).InsertDefinedThreeCheck($g(userID),curAdmLocID,episodeID)  //调用医生站,插入三级医师
	}
	s retCode=##class(Nur.NIS.Service.Base.Patient).UpdateMainNurse(episodeID,$g(mainNurIDArray),.errmsg)
	i +retCode'=0 d ret.SetAt("修改病人主管护士出错！","status") q ret    
    tc 
	q ##class(Nur.JSON).EncodeByWrite(ret)
}

/// Creator:      SongChao
/// CreateDate:   2017.5.24
/// Description:  获取病人结算状态
/// Input:
/// Return:
/// Other: 
ClassMethod GetCurrentDischargeStatus(episodeID)
{
	n (episodeID)
	q:episodeID="" ""
	s ret=""
	//E:医生出院  C:医生撤销出院 F:护士出院 R:护士召回病人  B:费用调整   T:结束费用调整
	s ret=##class(web.DHCDischargeHistory).GetCurrentDischargeStatus(episodeID)
	
	i ret="R" s ret="护士召回"
	i ret="E" s ret="医嘱出院 "
	i ret="C" s ret="医生撤销出院"
	i ret="F" s ret="护士出院"
	i ret="B" s ret="费用调整"
	i ret="T" s ret="结束费用调整"
	i ret="O" s ret="医嘱调整"
	i ret="DC" s ret="撤销出院"
	q ret
}

/// Creator:      SongChao
/// CreateDate:   2018.3.21
/// Description:  获取病人房间
/// Input:
/// Return:
/// Other:   w ##class(Nur.CommonInterface.Patient).getRoomDesc(16)
ClassMethod GetRoomDesc(episodeID)
{
	n (episodeID)
	s roomDesc=""
	s currRoomDR=$p(^PAADM(episodeID),"^",69)
	s currWardID=$p(^PAADM(episodeID),"^",70)
	s motherAdm=$p(^PAADM(episodeID),"^",75)
	s motherRoomDR=""
	i motherAdm'="" d
	.s currMotherWardID=$p(^PAADM(motherAdm),"^",70)
	.i currWardID=currMotherWardID s currRoomDR=$p(^PAADM(motherAdm),"^",69)
	.e  d
	..s transSub="" 
	..f  s transSub=$o(^PAADM(motherAdm,"TRANS",transSub),-1) q:(transSub="")!(motherRoomDR'="")  d
	...s transGlobal=^PAADM(motherAdm,"TRANS",transSub)
	...s transWardID=$p(transGlobal,"^",9)
	...q:transWardID'=currWardID
	...s bedDR=$p(transGlobal,"^",8)
	...q:bedDR=""
	...s motherRoomDR=$P(^PAWARD(+bedDR,"BED",$p(bedDR,"||",2)),"^",3)
	i motherRoomDR'="" s currRoomDR=motherRoomDR
	i currRoomDR'="" d
	.s roomData=$g(^PAROOM(currRoomDR))
	.q:roomData=""
	.s roomDesc= $p(roomData,"^",2)
	s nurTransAudit=$O(^Nur.DHCNurTransAuditI("AdmStatus"," "_episodeID," A",""))
	i nurTransAudit'="" d	
	.s roomDesc="转移中"
	q roomDesc
}

/// Creator: 		pengjunfu
/// CreatDate: 		2017.03-22
/// Description: 	判断是否有过敏
/// Input：			episodeID: 就诊号
/// Return：		 
/// Other           w ##class(Nur.CommonInterface.Patient).getIfAllergy("1")
ClassMethod GetIfAllergy(episodeID)
{
	n (episodeID)
	s patientID=+^PAADM(episodeID)
	s allergyActive=##class(web.PAAllergy).FindIfPatientHasAllergy(patientID)
	s allergyInd=##class(web.PAAllergy).FindIfPatientHasIndAllergy(patientID)
	s allergyTBC=##class(web.PAAllergy).GotTBCAllergy(patientID)
	q:(allergyActive=1)&(allergyInd="")&(allergyTBC="") 1
	q 0
}

/// Creator:      SongChao
/// CreateDate:   2017.6.28
/// Description:  传入就诊号判断病人是不是新生儿
/// Input:        EpisodeID
/// Return:       N:否,Y:是
/// Other: w ##class(Nur.CommonInterface.Patient).ifNewBaby("151")
ClassMethod IfNewBaby(episodeID)
{
	n (episodeID)
	q:episodeID=""
	s ifNewBaby="N"
	s motherADM =""
	s motherADM = $p(^PAADM(episodeID),"^",75)
	i motherADM'="" s ifNewBaby="Y"
	q ifNewBaby
}

/// Creator:      pengjunfu
/// CreateDate:   2018.5.11
/// Description:  获取性别
/// Input:	
/// Return:       
/// Other:        d ##class(%ResultSet).RunQuery("Nur.MNIS.Service.PatientInfo","findSex")
Query findSex() As websys.Query(ROWSPEC = "ID:%String,desc:%String")
{
}

ClassMethod findSexExecute(ByRef qHandle As %Binary) As %Status
{
	s repid=$i(^CacheTemp)
 	s ind=1
    s qHandle=$lb(0,repid,0)
    s ID=0 f  s ID=$o(^CT("SEX",ID)) q:ID=""  d
    .s desc=$p(^CT("SEX",ID),"^",2)
    .s startDate=$p(^CT("SEX",ID),"^",4)
    .s endDate=$p(^CT("SEX",ID),"^",5)
    .q:(startDate'="")&&(+$h<startDate)
    .q:(endDate'="")&&(+$h>endDate)
	.d OutPutFindSex
	q $$$OK
OutPutFindSex
	s Data=$lb(ID,desc)
 	s ^CacheTemp(repid,ind)=Data
 	s ind=ind+1
	q
}

/// Creator:      songchao
/// CreateDate:   2018.7.17
/// Description:  获取住院证ID
/// Input:	
/// Return:       
/// Other:        d ##class(Nur.CommonInterface.Patient).getIPBookID("151")
ClassMethod GetIPBookID(episodeID)
{
	n (episodeID)
	s IPBookID=""
	s IPBookID=$O(^DHCDocIPBK(0,"EpisodeIDTo",episodeID,""))
	q IPBookID
}

/// Creator:      songchao
/// CreateDate:   2018.7.17
/// Description:  获取性别
/// Input:	
/// Return:       
/// Other:        w ##class(Nur.CommonInterface.Patient).getIPAppBedID("2016")
ClassMethod GetIPAppBedID(episodeID)
{
	n (episodeID)
	s IPBookID=""
	s IPBookID=$O(^DHCDocIPBK(0,"EpisodeIDTo",episodeID,""))
	q:IPBookID="" ""
	s IPAppID=$o(^DHCDocIPApp(0,"IPBookingID",IPBookID,""))
	q:IPAppID="" ""
	s bedNODr=$p($g(^DHCDocIPApp(IPAppID)),"^",9)
	q bedNODr
}

/// Creator:      pengjunfu
/// CreateDate:   2018/6/22
/// Description:  获取术后天数
/// Input:
/// Return:       
/// Other:        ##Class(Nur.CommonInterface.Temperature).getOPDays(2194027,"64819")
ClassMethod GetOPDays(episodeID, date = "")
{
	n (episodeID, date)
	i date="" s date=+$h
	s day(2)="II"
	s day(3)="III"
    s day(4)="IV"
    s day(5)="V"
    s day(6)="Ⅵ"
    s day(7)="Ⅶ"
    s day(8)="Ⅷ"
    s opNum=""
    s opDay=""
    s ID=0  f  s ID=$o(^DHCADMQTREC("adm",episodeID,ID)) q:ID=""  d
	.s eventDate=$p(^DHCADMQTREC("QTREC",ID),"^",2)
	.s type=""
	.s typeID=$p(^DHCADMQTREC("QTREC",ID),"^",4)
	.i (typeID'="")&&($g(^DHCQTRECTYP("typ",typeID))'="") s type=$p(^DHCQTRECTYP("typ",typeID),"^",2)
	.q:type'="手术"
	.s day=date-eventDate
	.q:(day<0)!(day>14)
	.s opNum=+opNum+1
	.s opEvent(eventDate)=day
	s eventDate=$o(opEvent(""),-1)
	q:eventDate="" opNum
	q:opNum=1 opEvent(eventDate)
	q day(opNum)_"-"_opEvent(eventDate)
}

/// Creator:      songchao
/// CreateDate:   2018.12.
/// Description:  
/// Input:       
/// Return:       
/// Other:        ##Class(Nur.CommonInterface.Patient).getWeekDays()
ClassMethod GetWeekDays(episodeID)
{
	n (episodeID)
	s ^tempsc("getWeekDays")=$lb(episodeID)
	s ret=""
	s days=..PatInDays(episodeID)
	s week=+(+days\7)
	s day=+days#7
	i week>0 s ret="第"_week_"周"_"/第"_day_"天"
	e  s ret="第"_day_"天"
	q ret
}

/// Creator:      songchao
/// CreateDate:   2018.12.27
/// Description:  是否首次分床
/// Input:       
/// Return:       Y:是 N:否
/// Other:        ##Class(Nur.CommonInterface.Patient).IfFirstToBed()
ClassMethod IfFirstToBed(episodeID)
{
	n (episodeID)
	s ^tempsc("ifFirstToBed")=$lb(episodeID)
	Set firstToBed="Y",tmpTransSub=""
	f  s tmpTransSub=$o(^PAADM(episodeID,"TRANS",tmpTransSub)) q:(tmpTransSub="")||(firstToBed="N")  d
	.i +$p($g(^PAADM(episodeID,"TRANS",tmpTransSub)),"^",8)>0 s firstToBed="N"
	q firstToBed
}

/// Creator: 		songchao
/// CreatDate: 		2019.1.2
/// Description: 	获取病人护理级别颜色
/// Input：			episodeID: 就诊号
/// Return：		
/// Other           w ##class(Nur.CommonInterface.Patient).getCareLevelColor("161")
ClassMethod GetCareLevelColor(episodeID)
{
	n (episodeID)
	s colorCode=""
	s careLevelCodeS="CRITICALCARE^FIRSTCLSCARE^SECONDCLSCARE^THIRDCLSCARE"
	s count=$l(careLevelCodeS,"^")
	f index=1:1:count d
	.s careLevelCode=$p(careLevelCodeS,"^",index)
	.s hljb=##class(web.DHCSETIMAGE).IfOrddailyExistByConfig(episodeID,careLevelCode)
	.i hljb=1 d
	..s setID=$o(^Nur.ImageSetI("Code"," "_careLevelCode,""))
	..q:setID=""
	..s colorCode=$listget(^Nur.ImageSetD(setID),5)
	q colorCode
}

/// Creator: 		SongChao
/// CreatDate: 		2019.1.
/// Description: 	
/// Input：			
/// Return：		
/// Other 
ClassMethod GetCurrLocID(episodeID)
{
	n (episodeID)
	s ^tempsc("getCurrLocID")=$lb(episodeID)
	s currLocID=""
	s currLocID=$p($g(^PAADM(episodeID)),"^",4)
	s nurTransAuditID=""
	s nurTransAuditID=$o(^Nur.DHCNurTransAuditI("AdmStatus"," "_episodeID," A",""))
	q:nurTransAuditID="" currLocID
	s nurTransAudit=^Nur.DHCNurTransAuditD(nurTransAuditID)
	s currLocID=$lg(nurTransAudit,5)
	q currLocID
}

/// Creator: 		fengliang
/// CreatDate: 		2020.03.12
/// Description: 	PDA获取病人信息   
/// Input：			episodeID: 就诊号
/// Return：		{}
/// Other           w ##class(Nur.CommonInterface.Patient)
ClassMethod GetPatientPDA(episodeID) As %ArrayOfDataTypes [ CodeMode = objectgenerator ]
{
#;	d %code.WriteLine(" s patient={}")
	d %code.WriteLine(" s patient=##class(%ArrayOfDataTypes).%New()")
	d %code.WriteLine(" s personID=$p($g(^PAADM(episodeID)),""^"",1)")
	d %code.WriteLine(" s patientID=personID")
	d %code.WriteLine(" s admData=$g(^PAADM(episodeID))")
	d %code.WriteLine(" q:(episodeID="""")!(admData="""") patient")
	d %code.WriteLine(" s personID=$p(admData,""^"",1)")
	d %code.WriteLine(" s personData=$g(^PAPER(personID,""ALL""))")
	d %code.WriteLine(" q:personData="""" patient")
	for i=1:1:%compiledclass.Parameters.Count()
	{

		s origin=%compiledclass.Parameters.GetAt(i).Origin
		continue:origin'=%compiledclass.Name
		s name=%compiledclass.Parameters.GetAt(i).Name
		s description=%compiledclass.Parameters.GetAt(i).Description
		i description'="" s name=description
		continue:description=""
		s value=%compiledclass.Parameters.GetAt(i).Default  
		s value="""s "_name_"="_value_""""
		d %code.WriteLine(" x "_value)
		d %code.WriteLine(" d patient.SetAt("_name_","""_name_""")")
	}
	d %code.WriteLine(" q patient")
	q $$$OK
}

}
